// axios方法封装
import axios from 'axios';
import api from '../../api/index';
import router from '@/router/index';
import Vue from 'vue'

function isLoginApi(url) {
    if (url != api.login) {
        let authorization = localStorage.getItem('token') ? localStorage.getItem('token') : '';
        return authorization ? {
          authorization
        } : {}
    }
}

function dataHandler(type, url, data) {
    if (!url) {
        throw ('缺少url');
    }
    if (type === 'get') {
        var query = '';
        if (data) {
            query = '?'
            Object.keys(data).forEach(key => {
                query += `${key}=${data[key]}&`
            })
        }
        url = url + query.substring(0, query.length - 1);
        return {
            url
        }
    } else if (type === 'post') {
        return {
            url,
            data
        }
    }
}

class AxiosHandler {
    constructor(baseUrl, timeout) {
        // this.baseUrl = baseUrl;
        this.timeout = timeout;
        axios.defaults.baseUrl = baseUrl;
        axios.defaults.timeout = timeout;
        axios.interceptors.request.use((config) => {
            //  发送前拦截
            return config
        }, (err) => {
            return Promise.reject(err);
        })
    }

    get(url, data, header = {}) {
        return new Promise((resolved, reject) => {
            axios({
                method: 'get',
                ...dataHandler('get', url, data),
                headers: {
                    "Content-Type": "application/json; charset=UTF-8",
                    ...isLoginApi(url),
                    ...header
                },
            }).then((res) => {
                if (res.data.code === 200) {
                    resolved(res.data);
                } else if (res.data.code === 1002) {
                    Vue.prototype.$msg({
                        message: res.data.msg,
                        type: 'warning'
                    })
                    router.push('./login')
                } else {
                    Vue.prototype.$msg({
                        message: res.data.msg,
                        type: 'error'
                    })
                }
                // 根据状态码执行
            }).catch(err => {
                reject(err)
            })
        })
    }

    post(url, data, header = {}) {
        return new Promise((resolved, reject) => {
            axios({
                method: 'post',
                ...dataHandler('post', url, data),
                headers: {
                    "Content-Type": "application/json; charset=UTF-8",
                    ...isLoginApi(url),
                    ...header
                },
            }).then((res) => {
                if (res.data.code == 200) {
                    resolved(res.data);
                } else if (res.data.code == 1002) {
                    Vue.prototype.$msg({
                        message: res.data.msg,
                        type: 'warning'
                    })
                    router.push('./login')
                } else {
                    Vue.prototype.$msg({
                        message: res.data.msg,
                        type: 'error'
                    })
                }
                // 根据状态码执行
            }).catch(err => {
                reject(err)
            })
        })
    }

    put(url, data, header = {}) {
        return new Promise((resolved, reject) => {
            axios({
                method: 'put',
                ...dataHandler('post', url, data),
                headers: {
                    "Content-Type": "application/json; charset=UTF-8",
                    ...isLoginApi(url),
                    ...header
                },
            }).then((res) => {
                if (res.data.code === 200) {
                    resolved(res.data);
                } else if (res.data.code === 1002) {
                    Vue.prototype.$msg({
                        message: res.data.msg,
                        type: 'warning'
                    })
                    router.push('./login')
                } else {
                    Vue.prototype.$msg({
                        message: res.data.msg,
                        type: 'error'
                    })
                }
                // 根据状态码执行
            }).catch(err => {
                reject(err)
            })
        })
    }

    delete(url,data,header = {}){
        return new Promise((resolved, reject) => {
            axios({
                method: 'delete',
                ...dataHandler('get', url, data),
                headers: {
                    "Content-Type": "application/json; charset=UTF-8",
                    ...isLoginApi(url),
                    ...header
                },
            }).then((res) => {
                if (res.data.code === 0) {
                    resolved(res.data);
                } else if (res.data.code === -1) {
                    Vue.prototype.$msg({
                        message: res.data.msg,
                        type: 'warning'
                    })
                    router.push('./login')
                } else {
                    Vue.prototype.$msg({
                        message: res.data.msg,
                        type: 'error'
                    })
                }
                // 根据状态码执行
            }).catch(err => {
                reject(err)
            })
        })
    }
}

export default new AxiosHandler(api.baseUrl, 3000)